import express from 'express';
import dbhelp from "./../../lib/dbHelper";
import async from 'async';
import path from 'path';
import fs from 'fs';
const adminRouter=express.Router();
module.exports=adminRouter;
adminRouter.use("/index",(request,response,next) => {
    let menuSql = "SELECT * FROM menu WHERE m_status=1 ORDER BY m_order;";
    let websiteSql = "SELECT * FROM websiteinfo;";
    console.log(request.session["userinfo"]);
    async.parallel([
        function query1(fn){
            dbhelp.query(menuSql, [], function (err, res) {
                if (!err) {
                    fn(null,res);
                }
            })
        },
        function query2(fn){
            dbhelp.query(websiteSql, [], function (err, res) {
                if (!err) {
                    fn(null, res);
                }
            })
        }
    ],function(err,res){
        if(!err){
            response.render("./admin/index.html", {
                menu: res[0],
                website:res[1][0],
                user: request.session["userinfo"]
            });
            
        }
    })
})
adminRouter.get("/reg",(request,response,next) => {
    response.render("./admin/register.html");
})
adminRouter.post("/reg",(request,response,next) => {
    console.log(request.body);
    console.log(request.files[0]);
    let imgName = new Date().getTime() + path.extname(request.files[0].originalname);
    let newFileName = path.join(request.files[0].destination, imgName);
    console.log(newFileName);
    fs.rename(request.files[0].path, newFileName,(err)=>{
        if(!err){
            console.log("头像上传");
        }
    })
    let paramObj=request.body;
    let sql = "INSERT INTO userinfo (u_name,u_pwd,u_email,u_img) VALUES (?,?,?,?);";
    let params=[paramObj.uname,paramObj.upwd,paramObj.uemail,imgName];
    dbhelp.query(sql,params,(err,res) => {
        if (!err && res.affectedRows >= 1) {
            console.log(res);
            
                response.json({
                    msg:"注册成功",
                    status:1
                })
        }else{
            response.json({
                msg: "注册失败",
                status: 1
            })
        }
    })
    
})
adminRouter.get("/login", (request, response, next) => {
    response.render("./admin/login.html", {});
})
adminRouter.post("/login",(request,response,next) => {
    let adminObj=request.body;
    console.log(adminObj);
    let sql = "SELECT u_id,u_name,u_img FROM userinfo WHERE u_name=? AND u_pwd=? AND u_flag=1;";
    let params=[adminObj.uname,adminObj.upwd];
    dbhelp.query(sql,params,(err,res) => {
        if(!err && res.length>0){
            console.log(res);
            request.session["userinfo"] = res[0];
            response.json({
                msg: "登录成功",
                status: 1
            })
            
            
            
        }else{
            response.json({
                msg:"用户名或密码错误",
                status:-1
            })
        }
    })
})
adminRouter.get("/menu",(request,response,next) => {
    response.render("./admin/menu.html",{});
})
adminRouter.get("/getMenu",(request,response,next) => {
    let menuSql = "SELECT * FROM menu ORDER BY m_order;";
    dbhelp.query(menuSql, [], function (err, res) {
        let obj = {
            "code": 0,
            "msg": "",
            "count": res.length,
            "data": res
        }
        //console.log(obj);
        if (!err) {

            response.json(obj);
        }
    })
})
adminRouter.post("/delByMenuId",(request,response,next) => {
    let sql = "UPDATE menu SET m_status=0 WHERE m_id=?;";
    
    dbhelp.query(sql,[request.body.mid],function(err,res){
        if(!err&&res.affectedRows>=1){
            response.json({
                msg:"删除成功",
                status:1
            })
        }else{
            response.json({
                msg:"删除失败",
                status:-1
            })
        }
    })
})
adminRouter.post("/updateByMenuId",(request,response,next) => {
    let sql = "UPDATE menu SET m_name=?,m_url=?,m_num=?,m_parent=?,m_status=?,m_icon=?,m_order=? WHERE m_id=?;";
    let params=[
        request.body.m_name,
        request.body.m_url,
        request.body.m_num,
        request.body.m_parent,
        request.body.m_status,
        request.body.m_icon,
        request.body.m_order,
        request.body.m_id
    ];
    dbhelp.query(sql,params,function(err,res){
        if(!err&&res.affectedRows>=1){
            response.json({
                msg:"修改成功",
                status:1
            })
        }else{
            response.json({
                msg:"修改失败",
                status:-1
            })
        }
    })
})
adminRouter.post("/updateStatus",(request,response,next) => {
    let sql = "UPDATE menu SET m_status=? WHERE m_id=?;"
    let params=[request.body.status,request.body.mid];
    
    dbhelp.query(sql,params,(err,res) => {
        if(!err&&res.affectedRows>=1){
            response.json({
                msg:"状态已修改",
                status:1
            })
        }else{
            response.json({
                msg:"状态更新失败",
                status:-1,
                err:err
            })
        }
    })
})
adminRouter.get("/getMenuById",(request,response,next) => {
    let menuSql = "SELECT * FROM menu WHERE m_status=1 AND m_id=? ORDER BY m_order;";
    // console.log(request.query);
    dbhelp.query(menuSql, [request.query.mid], function (err, res) {
        let obj = {
            "code": 0,
            "msg": "",
            "count": res.length,
            "data": res
        }
        //console.log(obj);
        if (!err) {

            response.json(obj);
        }
    })
})
adminRouter.get("/addMenu",(request,response,next) => {
    response.render("./admin/addMenu.html",{});
})
adminRouter.post("/addMenu",(request,response,next) => {
    let sql = "INSERT INTO menu (m_name,m_url,m_num,m_parent,m_status,m_icon,m_order) VALUES (?,?,?,?,?,?,?);";
    let params=[
        request.body.m_name,
        request.body.m_url,
        request.body.m_num,
        request.body.m_parent,
        request.body.m_status,
        request.body.m_icon,
        request.body.m_order,
    ];
    function fn1(callback){
        dbhelp.query(sql, params, function (err, res) {
            if (!err&&res.affectedRows>=1) {
                callback(null,res);
            }else{
                callback(err, res);
            }
        })
    }
    async.parallel([fn1],function(err,res){
        console.log(err,res[0]);
        if (!err) {
            response.json({
                msg: "添加成功",
                status: 1
            })
        } else {
            response.json({
                msg: "添加失败",
                status: -1,
                err: err
            })
        }
    })

})